package work.mediway.mdm.biz.service.biz.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import work.mediway.mdm.biz.entity.biz.UserBTEventLog;
import work.mediway.mdm.biz.entity.patient.PaPatient;
import work.mediway.mdm.biz.mapper.biz.UserBTEventLogMapper;
import work.mediway.mdm.biz.service.biz.UserBTEventLogService;
import work.mediway.mdm.biz.vo.patient.StatisticsPatientNumVO;
import work.mediway.mdm.core.constant.JobConstant;
import work.mediway.mdm.biz.service.patient.PaPatientService;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/1/21
 */
@Transactional(rollbackFor = Exception.class)
@Service
@RequiredArgsConstructor
public class UserBTEventLogServiceImpl extends ServiceImpl<UserBTEventLogMapper, UserBTEventLog> implements UserBTEventLogService {

    private final PaPatientService paPatientService;

    @Override
    public void saveBtEventLog(UserBTEventLog userBtEventLog) {
        List<UserBTEventLog> list = this.baseMapper.listBtEventLog(userBtEventLog);
        /*this.list(Wrappers.lambdaQuery(UserBTEventLog.class)
                .eq(UserBTEventLog::getBtCode, userBtEventLog.getBtCode())
                .eq(UserBTEventLog::getLastUpdateDate, userBtEventLog.getLastUpdateDate()));*/


        if (CollUtil.isNotEmpty(list)) {
            this.baseMapper.update(userBtEventLog);
        } else {
            this.baseMapper.save(userBtEventLog);
        }
    }

    @Override
    public UserBTEventLog buildPatientEventLog(StatisticsPatientNumVO patientNumVO) {
        String[] date = StrUtil.splitToArray(DateUtil.now(), " ");
        String yesterday = DateUtil.formatDate(DateUtil.yesterday());
        UserBTEventLog userBtEventLog = new UserBTEventLog();
        userBtEventLog.setBtCode(JobConstant.CONST_PATIENT_KEY);
        // 查询当前ID
        String maxPatientId = this.paPatientService.maxPatientId();
        userBtEventLog.setBtCurrentId(maxPatientId);
        // 查询上一次ID后的新增数据
        UserBTEventLog yesterdayData = Optional.ofNullable(
                this.getOne(Wrappers.lambdaQuery(UserBTEventLog.class)
                        .eq(UserBTEventLog::getBtCode, userBtEventLog.getBtCode())
                        .eq(UserBTEventLog::getLastUpdateDate, yesterday).groupBy(UserBTEventLog::getLastUpdateDate))
        ).orElse(new UserBTEventLog());
        List<PaPatient> list = this.paPatientService.listLaterByPatientId(yesterdayData.getBtCurrentId());
        userBtEventLog.setBtIncrease(String.valueOf(list.size()));
        userBtEventLog.setBtTotalNum(String.valueOf(this.paPatientService.count()));
        userBtEventLog.setBtDetails(JSONUtil.toJsonStr(CollUtil.newArrayList(patientNumVO)));
        userBtEventLog.setLastUpdateDate(date[0]);
        userBtEventLog.setLastUpdateTime(date[1]);
        return userBtEventLog;
    }
}
